<?php
require_once '../include/config.php';
require_once '../include/utils.class.php';
require_once '../include/orm.class.php';
$orm = new ORM();

$action = $_POST['action'];

try {
  $orm->begin();
  switch ($action) {
    case 'miss_table':
      miss_table($orm);
      break;
    case 'miss_field':
      miss_field($orm);
      break;
    case 'diff_type':
      diff_type($orm);
      break;
    case 'red_table':
      red_table($orm);
      break;
    case 'red_field':
      red_field($orm);
      break;
    default:
      break;
  }
  $orm->commit();
} catch (PDOException $e) {
  Utils::dump($e); 
}

function miss_table($orm) {
  $table = $_POST['table'];
  $content = json_decode($_POST['content']);
  $orm->exec(__miss_table_sql($table, $content));
}

function miss_field($orm) {
  $table = $_POST['table'];
  $content = json_decode($_POST['content']);
  foreach ($content as $column => &$type) {
    $sql = "ALTER TABLE `$table` ADD `$column` " . __type_sql($type) . ';';
    $orm->exec($sql);
  }
  foreach ($content as $column => &$type) {
    if ($type->Key == 'MUL') {
      $sql = "CREATE INDEX $column ON $table ($column);";
      $orm->exec($sql);
    }
  }
}

function diff_type($orm) {
  $table = $_POST['table'];
  $content = json_decode($_POST['content']);
  foreach ($content as $column => &$type) {
    $sql = "ALTER TABLE `$table` MODIFY `$column` " . __type_sql($type) . ';';
    $orm->exec($sql);
  }
  
  foreach ($content as $column => &$type) {
    try {
      $sql = "CREATE INDEX $column ON $table ($column);";
      $orm->exec($sql);
    } catch (PDOException $ex) {
      
    }
    if ($type->Key != 'MUL') {
      $sql = "DROP INDEX $column ON $table;";
      $orm->exec($sql);
    }
  }
}

function red_table($orm) {
  $table = $_POST['table'];
  $content = json_decode($_POST['content']);
  $orm->exec('DROP TABLE IF EXISTS ' . $table);
}

function red_field($orm) {
  $table = $_POST['table'];
  $content = json_decode($_POST['content']);
  foreach ($content as $column => &$type) {
    $sql = "ALTER TABLE `$table` DROP `$column`;";
    $orm->exec($sql);
  }
}

function __miss_table_sql($table, $content) {
  $sql = "CREATE TABLE IF NOT EXISTS `$table` (";
  $total = count((array)$content);
  $count = 1;
  foreach ($content as $column => &$type) {
    $sql .= "`$column` " . __type_sql($type);
    if ($count != $total) {
      $sql .= ', ';
    }
    $count++;
  }
  $sql .= ') ENGINE=MYISAM;';
  foreach ($content as $column => &$type) {
    if ($type->Key == 'MUL') {
      $sql .= "CREATE INDEX $column ON $table ($column);";
    }
  }
  return $sql;
}

function __type_sql($type) {
  $sql = $type->Type;
  $sql .= ($type->Null == 'YES') ? '' : ' NOT NULL';
  if ($type->Extra == 'auto_increment') {
    $sql .= ' AUTO_INCREMENT';
  }
  if ($type->Key == 'PRI') {
    $sql .= ' PRIMARY KEY';
  }
  if ($type->Key == 'UNI') {
    $sql .= ' UNIQUE';
  }
  if ($type->Key != 'PRI' && $type->Type != 'text') {
    $sql .= ' DEFAULT \'' . $type->Default . '\'';
  }
  return $sql;
}
?>